package com.clas.liu.myapplication.view.activity.impl;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.presenter.impl.BasePresenter;
import com.clas.liu.myapplication.presenter.impl.EditPasswordPresenter;
import com.clas.liu.myapplication.view.activity.i.IEditPasswordActivity;
import com.clas.liu.myapplication.view.util.StringUtil;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by liu on 2019/5/2.
 */

public class EditPasswordActivity extends BaseActivity<EditPasswordPresenter> implements IEditPasswordActivity,View.OnClickListener{

    private ImageView back;
    private ImageView submit;
    private EditText phoneEt;
    private EditText codeEt;
    private TextView getCodeView;
    private EditText pwdEt;
    private EditText againPwdEt;
    private ProgressBar loading;

    private CountDownTimer timer;
    private EventHandler smsHandler;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password_layout);
        findView();
        setListener();
        init();
    }

    private void init() {
        timer = new CountDownTimer(60000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                getCodeView.setClickable(false);
                getCodeView.setTextColor(getResources().getColor(R.color.font_grey));
                String text = "重发" + "(" + String.valueOf(millisUntilFinished / 1000) + "s)";
                getCodeView.setText(text);
            }

            @Override
            public void onFinish() {
                getCodeView.setClickable(true);
                getCodeView.setText("发送验证码");
                getCodeView.setTextColor(getResources().getColor(R.color.login_blue));
            }
        };
        smsHandler = new EventHandler() {
            @Override
            public void afterEvent(final int event, int result, Object data) {
                if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    if (result == SMSSDK.RESULT_COMPLETE) {
                        // TODO 处理成功得到验证码的结果
                        // 请注意，此时只是完成了发送验证码的请求，验证码短信还需要几秒钟之后才送达
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                timer.start();
                            }
                        });
                    } else {
                        // TODO 处理错误的结果
                        ((Throwable) data).printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showToast("验证码获取异常", Toast.LENGTH_SHORT);
                            }
                        });
                    }
                } else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    if (result == SMSSDK.RESULT_COMPLETE) {
                        // TODO 处理验证码验证通过的结果
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mPresenter.editPassword(phoneEt.getText().toString(),pwdEt.getText().toString());
                            }
                        });
                    } else {
                        // TODO 处理错误的结果
                        ((Throwable) data).printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showToast("验证码错误", Toast.LENGTH_SHORT);
                                hideLoadingView();
                            }
                        });
                    }
                }
            }
        };
        SMSSDK.registerEventHandler(smsHandler);
    }

    private void setListener() {
        back.setOnClickListener(this);
        submit.setOnClickListener(this);
        getCodeView.setOnClickListener(this);
    }

    private void findView() {
        back = findViewById(R.id.edit_password_back_bt);
        submit = findViewById(R.id.edit_password_submit);
        phoneEt = findViewById(R.id.password_edit_phone);
        codeEt = findViewById(R.id.edit_password_et_password_code);
        getCodeView = findViewById(R.id.edit_password_get_code_tv);
        pwdEt = findViewById(R.id.password_edit_new_pwd);
        againPwdEt = findViewById(R.id.password_edit_new_pwd_again);
        loading = findViewById(R.id.edit_password_submit_pb);
    }

    @Override
    protected EditPasswordPresenter getPresenter() {
        return new EditPasswordPresenter(this);
    }

    @Override
    public void failBecauseNetReturn(int code) {
        hideLoadingView();
        showToast(code+"请求失败!",Toast.LENGTH_SHORT);
    }

    @Override
    public void postReturn(int code, String message) {
        hideLoadingView();
        if (code == 0) {
            showToast("新密码设置成功!",Toast.LENGTH_SHORT);
            this.finish();
        }else
            showToast(message,Toast.LENGTH_SHORT);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edit_password_back_bt:
                this.finish();
                break;
            case R.id.edit_password_submit:
                checkInputAndPost();
                break;
            case R.id.edit_password_get_code_tv:
                checkPhoneAndGetCode();
                break;
        }

    }

    private void checkInputAndPost() {
        showLoadingView();
        String phone = phoneEt.getText().toString();
        String newPwd = pwdEt.getText().toString();
        String againPwd = againPwdEt.getText().toString();
        String code = codeEt.getText().toString();

        if (pwdEt.length() < 6 || pwdEt.length() > 15) {
            showToast("密码过长或过短！",Toast.LENGTH_SHORT);
            hideLoadingView();
            return;
        }
        if (!newPwd.equals(againPwd)) {
            showToast("两次输入密码不一致！",Toast.LENGTH_SHORT);
            hideLoadingView();
            return;
        }
        SMSSDK.submitVerificationCode("86",phone,code);
    }

    private void checkPhoneAndGetCode() {
        String phoneNum = phoneEt.getText().toString();
        if (StringUtil.isMobile(phoneNum)) {
            SMSSDK.getVerificationCode("86", phoneNum);
        } else {
            showToast("请输入正确格式的手机号", Toast.LENGTH_SHORT);
        }
    }

    public void showLoadingView() {
        loading.setVisibility(View.VISIBLE);
        submit.setVisibility(View.GONE);
    }

    public void hideLoadingView() {
        loading.setVisibility(View.GONE);
        submit.setVisibility(View.VISIBLE);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(smsHandler);
    }
}
